package com.lagou.homework.stage01.module04.copyFile;

import java.io.*;
import java.util.concurrent.Callable;

public class CopyFileThread implements Callable {

    private String sPath;
    private String dPath;

    public CopyFileThread(String sPath, String dPath) {
        this.sPath = sPath;
        this.dPath = dPath;
    }

    @Override
    public Object call() throws Exception {
        BufferedInputStream bis = null;
        BufferedOutputStream bos =null;

        try {
            File file = new File(dPath);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }
            if(!file.exists()){
                file.createNewFile();
            }

            bis = new BufferedInputStream(new FileInputStream(sPath));
            bos = new BufferedOutputStream(new FileOutputStream(dPath));

            System.out.println("Coping file "+sPath);

            byte[] bArr = new byte[1024];
            int res = 0;
            while((res=bis.read(bArr))!=-1){
                bos.write(bArr,0,res);
            }
            System.out.println("Copy file SUCCESS !");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null!=bos){
                bos.close();
            }
            if(null!=bis){
                bis.close();
            }
        }
        return null;
    }
}
